<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>个人信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="login/bootstrap.min.css" rel="stylesheet">
    <script src="login/jquery.min.js"></script>
    <script src="login/bootstrap.min.js"></script>
    <script src="login/layer.js"></script>
</head>
<body>

<h2>个人信息</h2>

<form id="userForm" class="form-horizontal" role="form">

    <input type="hidden" class="form-control" id="id" name="id" th:value="${user.id}">

    <div class="form-group">
        <label for="loginid" class="col-sm-2 control-label form-inline">登录id：</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="loginid" name="loginid" th:value="${user.loginid}"/>
        </div>
    </div>
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label form-inline">用户名：</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="username" name="username" th:value="${user.username}"
                   placeholder="请输入用户名">
        </div>
    </div>
    <div class="form-group">
        <label for="mobile" class="col-sm-2 control-label">手机号码：</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="mobile" name="mobile" th:value="${user.mobile}"
                   placeholder="请输入手机号码">
        </div>
    </div>
    <div class="form-group">
        <label for="weixinid" class="col-sm-2 control-label">微信id：</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="weixinid" name="weixinid" th:value="${user.weixinid}"
                   placeholder="请输入微信id">
        </div>
    </div>
    <div class="form-group">
        <label for="email" class="col-sm-2 control-label">邮箱：</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="email" name="email" th:value="${user.email}"
                   placeholder="请输入邮箱">
        </div>
    </div>
</form>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-primary" onclick="updateUserInfo()">保存</button>
        <button class="btn btn-primary" data-toggle="modal" data-target="#myModal">修改密码</button>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">修改密码</h4>
            </div>
            <div class="modal-body">
                <form id="passwordForm" class="form-horizontal" role="form">
                    <input type="hidden" class="form-control" id="id" name="id" th:value="${user.id}">
                    <div class="form-group">
                        <label for="old_pssword" class="col-sm-2 control-label">原密码</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="old_pssword" name="old_pssword"
                                   placeholder="请输入原密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="new_pssword" class="col-sm-2 control-label">新密码</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_pssword" name="new_pssword"
                                   placeholder="请输入新密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="confirm_pssword" class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="confirm_pssword" name="confirm_pssword"
                                   placeholder="请输入确认密码">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="updatePassword()">提交</button>
            </div>
        </div>
    </div>
</div>

<script>

    function updateUserInfo() {

        if(!check()){
            return;
        }

        $.ajax({
            type: "post",
            dataType: "json",
            url: "saveUser",
            data: $('#userForm').serialize(),
            success: function (result) {
                alert(JSON.stringify(result));
            },
            error: function () {
                alert("异常！");
            }
        });
    }

    function updatePassword() {
        $.ajax({
            type: "post",
            dataType: "json",
            url: "savePassword",
            data: $('#passwordForm').serialize(),
            success: function (result) {
                alert(JSON.stringify(result));
            },
            error: function () {
                alert("异常！");
            }
        });
    }

    function check() {
        if(!isPhone($("#mobile").val())){
            layer.msg('手机号码格式错误', {icon: 5, time: 2000}, function (index) {
                layer.close(index);
                $('#mobile').focus();
            });
            return false;
        }
        if(!isEmail($("#email").val())){
            layer.msg('邮箱格式错误', {icon: 5, time: 2000}, function (index) {
                layer.close(index);
                $('#email').focus();
            });
            return false;
        }
        return true;
    }

    function isPhone(phone) {
        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if (!myreg.test(phone)) {
            return false;
        } else {
            return true;
        }
    }

    function isEmail(email) {
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

        if (!myreg.test(email)) {
            return false;
        } else {
            return true;
        }
    }

</script>

</body>
</html>